package editor.manager;

import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import editor.Context;
import editor.Mode;
import editor.layout.dialog.AboutDialog;
import editor.layout.dialog.BuildProjectDialog;
import editor.layout.dialog.CreateNewFileDialog;
import editor.layout.dialog.CreateNewProjectDialog;
import editor.layout.dialog.DeleteFileDialog;
import editor.layout.dialog.ExecuteProjectDialog;
import editor.layout.dialog.WorkspaceDialog;

/**
 * Manages the menu-bar
 * 
 * @author Mark Dessain
 */
@SuppressWarnings("serial")
public class MenuManager extends JMenuBar implements ActionListener, Manager
{

	// The Menu Root items
	private JMenu file = new JMenu("File");
	private JMenu edit = new JMenu("Edit");
	private JMenu project = new JMenu("Project");
	private JMenu help = new JMenu("Help");
    
	// The items which belong in the file menu
	private JMenuItem fileNewProject = new JMenuItem("New Project");
	private JMenuItem fileNewClass = new JMenuItem("New Class");
	private JMenuItem fileOpenFile  = new JMenuItem("Open File");
	private JMenuItem fileClose = new JMenuItem("Close");
	private JMenuItem fileCloseAll = new JMenuItem("Close All");
	private JMenuItem fileSave = new JMenuItem("Save");
	private JMenuItem fileSaveAs = new JMenuItem("Save As");
	private JMenuItem fileSaveAll = new JMenuItem("Save All");
	private JMenuItem fileRefresh = new JMenuItem("Refresh");
	private JMenuItem fileChangeWorkspace  = new JMenuItem("Change Workspace");
	private JMenuItem fileExit = new JMenuItem("Exit");
	
	// The items which belong in the edit menu
	private JMenuItem editUndo = new JMenuItem("Undo");
	private JMenuItem editRedo = new JMenuItem("Redo");	
	private JMenuItem editDelete = new JMenuItem("Delete");
	
	// The items which belong in the project menu
	private JMenuItem projectBuild = new JMenuItem("Compile Project");
	private JMenuItem projectRun = new JMenuItem("Execute Project");
	
	// The items which belong in the help menu
	private JMenuItem helpAbout = new JMenuItem("About");
	
    
	/**
	 * Menu Manager
	 */
	public MenuManager()
	{
		// Add items to the appropriate menus
		fileMenu();
		editMenu();
		projectMenu();
		helpMenu();
	}
	

	/**
	 * Set-up the File Menu with items, shortcuts and actions
	 */
	private void fileMenu()
	{
        add(file);
        
        // Add MenuItems
        file.add(fileNewProject);
        file.add(fileNewClass);
        file.add(fileOpenFile);
        file.addSeparator();
        file.add(fileClose);
        file.add(fileCloseAll);
        file.addSeparator();
        file.add(fileSave);
        file.add(fileSaveAs);
        file.add(fileSaveAll);
        file.addSeparator();
        file.add(fileRefresh);
        file.add(fileChangeWorkspace);
        file.addSeparator();
        file.add(fileExit);
        
        fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));
        fileSaveAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK + Event.SHIFT_MASK));
        fileRefresh.setAccelerator(KeyStroke.getKeyStroke("F5"));
        fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,Event.CTRL_MASK));
        fileCloseAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,Event.CTRL_MASK + Event.SHIFT_MASK));
        
        // Set-up Listener
        fileNewProject.addActionListener(this);
        fileNewClass.addActionListener(this);
        fileClose.addActionListener(this);
        fileCloseAll.addActionListener(this);
        fileSave.addActionListener(this);
        fileSaveAs.addActionListener(this);
        fileSaveAll.addActionListener(this);
        fileRefresh.addActionListener(this);
        fileChangeWorkspace.addActionListener(this);
        fileExit.addActionListener(this);
	}
	
	/**
	 * Set-up the Edit Menu with items, shortcuts and actions
	 */
	private void editMenu()
	{
        add(edit);
        
        // Add MenuItems
        edit.add(editUndo);
        edit.add(editRedo);
        edit.add(editDelete);
        

        editUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK));
        editRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK));
        editDelete.setAccelerator(KeyStroke.getKeyStroke("DELETE"));
        
        editUndo.addActionListener(this);
        editRedo.addActionListener(this);
        editDelete.addActionListener(this);
	}

	
	/**
	 * Set-up the Project Menu with items, shortcuts and actions
	 */
	private void projectMenu()
	{
        add(project);
        
        // Add MenuItems
        project.add(projectBuild);
        project.add(projectRun);
        
        projectBuild.addActionListener(this);
        projectRun.addActionListener(this);
	}
	
	/**
	 * Set-up th Help Menu with items, shortcuts and actions
	 */
	private void helpMenu()
	{
        add(help);
        
        // Add MenuItems
        help.add(helpAbout);
        
        helpAbout.addActionListener(this);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{	
			
		// Select the source
		Object source = e.getSource();
		
		if(source == fileNewProject)
		{
			// Display a New Project Dialog box
			new CreateNewProjectDialog();
		}	
		else if(source == fileNewClass)
		{
			// Display a New Project Dialog box
			new CreateNewFileDialog();
		}	
		else if(source == fileClose)
		{
			// Remove the current open tab
			WorkSpaceManager.textAreaManager.remove(WorkSpaceManager.textAreaManager.getSelectedComponent());
		}		
		else if(source == fileCloseAll)
		{
			// Remove all the open tabs
			WorkSpaceManager.textAreaManager.removeAll();
		}			
		else if(source == fileSave)
		{
			// Save the current open tab
			WorkSpaceManager.textAreaManager.save(WorkSpaceManager.textAreaManager.getSelectedComponent());
		}			
		else if(source == fileSaveAs)
		{
			// TODO missing action
		}			
		else if(source == fileSaveAll)
		{
			// Saves all the open tabs
			WorkSpaceManager.textAreaManager.saveAll();
		}	
		else if(source == fileRefresh)
		{
			// Reload the whole settings and project list
			Context.getEditorSettings().reload();
			Context.getSideBarManager().getProjectManager().reload();
		}
		else if(source == fileChangeWorkspace)
		{
			// Display a Change Workspace Dialog box
			new WorkspaceDialog();
		}
		else if(source == fileExit)
		{
			// Dispose of Main, closes program
			Context.getJwalkEditor().dispose();
		}	
		else if(source == editUndo)
		{
			WorkSpaceManager.textAreaManager.getSelectedEditor().undo();
		}	
		else if(source == editRedo)
		{
			WorkSpaceManager.textAreaManager.getSelectedEditor().redo();
		}
		else if(source == editDelete)
		{
			new DeleteFileDialog(Context.getSideBarManager().getProjectManager().getSelectedNode());
		}
		else if(source == projectBuild)
		{
			new BuildProjectDialog();
		}
		else if(source == projectRun)
		{
			new ExecuteProjectDialog();
		}
		else if(source == helpAbout)
		{
			new AboutDialog();
		}
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#switchMode(editor.Mode)
	 */
	@Override 
	public void switchMode(Mode mode)
	{
		if(mode == Mode.CODING)
		{
			codingMode();
		}
		else if(mode == Mode.QUESTION)
		{
			questionMode();	
		}
		else if(mode == Mode.TESTING)
		{
			testingMode();
		}
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#codingMode()
	 */
	@Override
	public void codingMode() 
	{
		projectBuild.setEnabled(true);
		projectRun.setEnabled(true);	
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#questionMode()
	 */
	@Override
	public void questionMode() 
	{
		projectBuild.setEnabled(false);	
		projectRun.setEnabled(false);
	}
	
	/* (non-Javadoc)
	 * @see editor.manager.Manager#testingMode()
	 */
	@Override
	public void testingMode() 
	{
		projectBuild.setEnabled(false);	
		projectRun.setEnabled(true);
	}



}
